/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-08-23 15:03:12
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-25 13:00:33
 */
import React, {useState} from 'react';
import { DownOutlined } from '@ant-design/icons';
import { Dropdown, Menu, Space, Button, message } from 'antd';
import request from "@/utils/request";
import cmdbres from "@/data/cmdbres";
import SimpModule from './simpModule';
import IdleMachineModule from "./idleMachineModule";
import BusinessModule from './businessModule';
import HostPoolModule from "./hostPoolModule";

const RelationBizSelect = ({assign, rowKeys, getTableData, cmModuleId}) => {
    const [moduleType, setModuleType] = useState();
    const [visible, setVisible] = useState(false);
    const [bizInfo, setBizInfo] = useState();
    const [serviceInfo, setServiceInfo] = useState(new Map());

    const onClick = async({ key }) => {
        if(!(rowKeys && rowKeys.length)) {
            message.warning(`请先选择需要${assign?'分配':'转移'}的主机`);
            return;
        }
        if(key != "simp") {//分配到业务空闲机池，不需要查询主机的关联信息
            const res = await queryResourceForServer();
            if(!(res && res.code == 200 && res.data)) return;
            if(!res.data.isOneBiz) {
                message.warning("仅支持对相同业务下的主机进行操作");
                return;
            }
            setServiceInfo(res.data.servers);
            // getServiceInstance(res.data.servers);
            //查找业务
            const bizObj = res.data.servers?.[0]?.bizInfo?.[0];
            if(!bizObj) { // 空闲机需要先找到业务
                message.warning("请确认已分配到业务！");
                return;
            }
            setBizInfo(bizObj);
        }
        
        setVisible(true);
        setModuleType(key);
    };

    const getServiceInstance = (data) => {
        let map = new Map();
        rowKeys?.map(key=> {
            let obj = data?.find(item=> item?.server?.cm_unid == key);
            obj.serviceInstances?.map(item=> item.serverId == key);//把主机ID存到模块信息中，转移时需要此数据
            map.set(key, obj);
        })
        setServiceInfo(map);
    };

    const assignMenu = (
        <Menu onClick={onClick}>
            <Menu.Item key="simp">业务空闲机池</Menu.Item>
        </Menu>
    );

    const moveToMenu = (
        <Menu onClick={onClick}>
            <Menu.Item key="idleMachine">空闲机</Menu.Item>
            <Menu.Item key="businessModule">业务模块</Menu.Item>
            <Menu.Item key="hostPool">主机池</Menu.Item>
        </Menu>
    );

    const queryResourceForServer = async() => {
        try {
            const res = await request({
                ...cmdbres.queryResourceForServer,
                data: rowKeys,
            });
            return res;
        } catch (error) {
            console.log(error);
        } 
    }

    return (
        <>
            <Dropdown 
                overlay={assign ? assignMenu: moveToMenu} 
                trigger={['click']}
            >
                <Button>
                    <Space>
                    {assign ? "分配到": "转移至"} 
                    <DownOutlined />
                    </Space>
                </Button>
            </Dropdown>
            {moduleType == "simp" && 
                <SimpModule
                    visible={visible}
                    setVisible={setVisible}
                    rowKeys={rowKeys}
                    onFinish={(flag)=> {if(flag && getTableData) getTableData() }}
                    cmModuleId={cmModuleId}
                />
            }
            {moduleType == "idleMachine" && 
                <IdleMachineModule
                    visible={visible}
                    setVisible={setVisible}
                    rowKeys={rowKeys}
                    bizInfo={bizInfo}
                    serviceInfo={serviceInfo}
                    onFinish={(flag)=> {if(flag && getTableData) getTableData() }}
                    cmModuleId={cmModuleId}
                />
            }
            {
                moduleType == "businessModule" && 
                <BusinessModule
                    visible={visible}
                    setVisible={setVisible}
                    rowKeys={rowKeys}
                    bizInfo={bizInfo}
                    serviceInfo={serviceInfo}
                    onFinish={(flag)=> {if(flag && getTableData) getTableData() }}
                    cmModuleId={cmModuleId}
                />
            }
            {
                moduleType == "hostPool" && 
                <HostPoolModule
                    visible={visible}
                    setVisible={setVisible}
                    rowKeys={rowKeys}
                    bizInfo={bizInfo}
                    onFinish={(flag)=> {if(flag && getTableData) getTableData() }}
                    cmModuleId={cmModuleId}
                />
            }
            
        </>
    )
};

export default RelationBizSelect;